﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace Filemanager
{
    public class Process
    {
        private string url;
        public static int Length; 
        public string TreeFolder(string root, int view)
        {
            List<Folder> folderlist = new List<Folder>();
            duyetThuMuc(root, folderlist);
            string t = GetHTML(folderlist[0], view);
            return GetHTML(folderlist[0], view);

        }
        public string GetHTML(Folder f, int view)
        {
            string s = "";
            s += "<ul>";
            if (f.ItemFolder.Count != 0)
            {
                for (int i = 0; i < f.ItemFolder.Count; i++)
                {
                    s += "<li>";
                    s += "<span class=\"toggle\"></span>";
                    if (view == 1)
                    {
                        s += "<a href=\"Viewthumb.aspx?root=" + f.ItemFolder[i].URL + "\"";
                    }
                    else if (view == 2)
                    {
                        s += "<a href=\"ViewList.aspx?root=" + f.ItemFolder[i].URL + "\"";
                    }
                    else
                    {
                        s += "<a href=\"ViewDetail.aspx?root=" + f.ItemFolder[i].URL + "\"";
                    }
                    s += " class=\"folder\"><span>" + f.ItemFolder[i].Name + "</span></a>";
                    if (f.ItemFolder[i].ItemFolder.Count != 0 || f.ItemFolder[i].File.Count != 0)
                    {
                        s += GetHTML(f.ItemFolder[i], view);
                    }
                    s += "</li>";
                }
            }
            if (f.File.Count != 0)
            {
                for (int i = 0; i < f.File.Count; i++)
                {
                    s += "<li><a href=\"#\" class=\"document-web\"><span>" + cutName(f.File[i]) + "</span></a></li>";
                }
            }
            s += "</ul>";
            return s;
        }
        public void duyetThuMuc(string root, List<Folder> folderlist)
        {
            string[] folders = Directory.GetDirectories(root);
            string[] files = Directory.GetFiles(root);
            Folder f = new Folder();
            f.ItemFolder = new List<Folder>();
            f.File = new List<string>();
            f.SizeFile = new List<int>();
            f.CreateTime = new List<DateTime>();
            f.Name = cutName(root);
            f.URL = GetURLFile(Length, root);
            f.CreateTimeFolder = CreateTimeFolder(root);
            url = CutURL(root);
            for (int i = 0; i < files.Length; i++)
            {
                f.SizeFile.Add(SizeFile(files[i]));
                f.CreateTime.Add(CreateTime(files[i]));
                f.File.Add(ChangURLFile("/Mydocuments" + GetURLFile(Length, files[i])));
            }
            for (int i = 0; i < folders.Length; i++)
            {
                duyetThuMuc(folders[i], f.ItemFolder);
            }
            folderlist.Add(f);
        }
        public string cutName(string name)
        {
            string s = "";
            for (int i = name.Length - 1; i >= 0; i--)
            {
                if (name[i] != '\\' && name[i] != '/')
                {
                    s += name[i];
                }
                else
                {
                    break;
                }
            }
            name = "";
            for (int i = s.Length - 1; i >= 0; i--)
            {
                name += s[i];
            }
            return name;
        }
        public string GetExtenFile(string name)
        {
            string s = "";
            for (int i = name.Length - 1; i >= 0; i--)
            {
                if (name[i] != '.')
                {
                    s += name[i];
                }
                else
                {
                    break;
                }
            }
            name = "";
            for (int i = s.Length - 1; i >= 0; i--)
            {
                name += s[i];
            }
            return name;
        }
        public string CutURL(string url)
        {
            string s = "";
            int temp = 0;
            for (int i = url.Length - 1; i >= 0; i--)
            {
                if (url[i] == '\\')
                {
                    temp = i;
                    break;
                }
            }
            for (int i = 0; i < temp; i++)
            {
                s += url[i];
            }
            return s;
        }
        public string GetURLFile(int t, string url)
        {
            string s = "";
            for (int i = t; i < url.Length; i++)
            {
                s += url[i];
            }
            return s;
        }
        public string ChangURLFile(string url)
        {
            string s = "";
            for (int i = 0; i < url.Length; i++)
            {
                if (url[i] == '\\')
                {
                    s += '/';
                }
                else
                {
                    s += url[i];
                }
            }
            return s;
        }
        public int SizeFile(string url)
        {
            FileInfo file = new FileInfo(url);
            return (int)file.Length / 1024;
        }
        public DateTime CreateTime(string url)
        {
            FileInfo file = new FileInfo(url);
            return file.CreationTime;
        }
        public DateTime CreateTimeFolder(string url)
        {
            DirectoryInfo dir = new DirectoryInfo(url);
            return dir.CreationTime;
        }
    }
}